package com.example.wechattest;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

public class WeChat extends AppCompatActivity implements View.OnClickListener {

    private ImageButton mBtnWeChat;
    private ImageButton mBtnContact;
    private ImageButton mBtnFind;
    private ImageButton mBtnMe;
    private TextView mTextWeChat;
    private TextView mTextContact;
    private TextView mTextFind;
    private TextView mTextMe;

    private WeChatFragment mWeChatFragment = null;
    private ContactFragment mContactFragment = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);

        mBtnWeChat = findViewById(R.id.btn_wechat);
        mBtnContact = findViewById(R.id.btn_contact);
        mBtnFind = findViewById(R.id.btn_find);
        mBtnMe = findViewById(R.id.btn_me);

        mTextWeChat = findViewById(R.id.text_wechat);
        mTextContact = findViewById(R.id.text_contact);
        mTextFind = findViewById(R.id.text_find);
        mTextMe = findViewById(R.id.text_me);

        mBtnWeChat.setOnClickListener(this);
        mBtnContact.setOnClickListener(this);
        mBtnFind.setOnClickListener(this);
        mBtnMe.setOnClickListener(this);
        selectTab(0);

    }

    @Override
    public void onClick(View v) {
        setTabDefaultColor();
        switch (v.getId()) {
            case R.id.btn_wechat:
                mBtnWeChat.setImageResource(R.mipmap.weixin_pressed);
                mTextWeChat.setTextColor(0xFF45C01A);
                selectTab(0);
                break;
            case R.id.btn_contact:
                mBtnContact.setImageResource(R.mipmap.contact_list_pressed);
                mTextContact.setTextColor(0xFF45C01A);
                selectTab(1);
                break;
            case R.id.btn_find:
                mBtnFind.setImageResource(R.mipmap.find_pressed);
                mTextFind.setTextColor(0xFF45C01A);
                selectTab(2);
                break;
            case R.id.btn_me:
                mBtnMe.setImageResource(R.mipmap.profile_pressed);
                mTextMe.setTextColor(0xFF45C01A);
                selectTab(3);
                break;
        }
    }

    public void selectTab(int index) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if (mWeChatFragment != null) {
            transaction.hide(mWeChatFragment);
        }
        if (mContactFragment != null) {
            transaction.hide(mContactFragment);
        }
        switch (index) {
            case 0:
                if (mWeChatFragment == null) {
                    mWeChatFragment = new WeChatFragment();
                    transaction.add(R.id.fl_container , mWeChatFragment);
                }
                else {
                    transaction.show(mWeChatFragment);
                }
                break;

            case 1:
                if (mContactFragment == null) {
                    mContactFragment = new ContactFragment();
                    transaction.add(R.id.fl_container, mContactFragment);
                }
                else {
                    transaction.show(mContactFragment);
                }
                break;
        }
        transaction.commit();
    }

    private void setTabDefaultColor() {
        mBtnWeChat.setImageResource(R.mipmap.weixin_normal);
        mBtnContact.setImageResource(R.mipmap.contact_list_normal);
        mBtnFind.setImageResource(R.mipmap.find_normal);
        mBtnMe.setImageResource(R.mipmap.profile_normal);
        mTextWeChat.setTextColor(0xFFFFFFFF);
        mTextContact.setTextColor(0xFFFFFFFF);
        mTextFind.setTextColor(0xFFFFFFFF);
        mTextMe.setTextColor(0xFFFFFFFF);
    }
}
